package com.excesys.exsecs.energy.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.energy.entity.EnergyRateFlow;
import com.excesys.exsecs.energy.entity.EnergyRateManage;
import com.excesys.exsecs.energy.mapper.EnergyRateManageMapper;
import com.excesys.exsecs.energy.service.EnergyRateFlowService;
import com.excesys.exsecs.energy.service.EnergyRateManageService;
import com.excesys.exsecs.energy.vo.EnergyRateManageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 能源费率管理
 *
 * @author zhang
 * @date 2024-09-27 10:29:22
 */
@Service
public class EnergyRateManageServiceImpl extends ServiceImpl<EnergyRateManageMapper, EnergyRateManage> implements EnergyRateManageService {

    @Autowired
    private EnergyRateFlowService energyRateFlowService;

    @Override
    public Page<EnergyRateManage> queryPage(Page page, EnergyRateManage rateManage) {
        Page<EnergyRateManage> selectPage = this.baseMapper.selectPage(page, Wrappers.<EnergyRateManage>lambdaQuery()
                .like(StringUtils.isNotBlank(rateManage.getRateName()), EnergyRateManage::getRateName, rateManage.getRateName())
                .eq(StringUtils.isNotBlank(rateManage.getRateType()), EnergyRateManage::getRateType, rateManage.getRateType())
                .eq(StringUtils.isNotBlank(rateManage.getEnergyType()), EnergyRateManage::getEnergyType, rateManage.getEnergyType())
                .orderByDesc(EnergyRateManage::getCreateTime));
        selectPage.getRecords().forEach(rate -> {
            List<EnergyRateFlow> flowList = energyRateFlowService.getBaseMapper().selectList(Wrappers.<EnergyRateFlow>lambdaQuery().eq(EnergyRateFlow::getRateId, rate.getRateId()));
            rate.setFlowList(flowList);
        });
        return selectPage;
    }


    @Override
    public boolean saveAndUpdate(EnergyRateManage energyRateManage) {
        if (energyRateManage != null) {
            if (energyRateManage.getId() != null) {
                //修改
                this.baseMapper.updateById(energyRateManage);
                if (energyRateManage.getFlowList() != null) {
                    energyRateFlowService.getBaseMapper().delete(Wrappers.<EnergyRateFlow>lambdaQuery().eq(EnergyRateFlow::getRateId, energyRateManage.getRateId()));
                    energyRateManage.getFlowList().forEach(flow -> {
                        flow.setRateId(energyRateManage.getRateId());
                        energyRateFlowService.save(flow);
                    });
                }
            } else {
                //新增
                String formatDateTime2 = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
                String rateId = "FL" + formatDateTime2 + RandomUtil.randomString(5);
                energyRateManage.setRateId(rateId);
                this.baseMapper.insert(energyRateManage);
                if (energyRateManage.getFlowList() != null) {
                    energyRateManage.getFlowList().forEach(flow -> {
                        flow.setRateId(rateId);
                        energyRateFlowService.save(flow);
                    });
                }
            }
        }
        return true;
    }

    @Override
    public List<EnergyRateManageVo> export(EnergyRateManage energyRateManage) {
        List<EnergyRateManageVo> resultList = new ArrayList<>();
        List<EnergyRateManage> selectPage = this.baseMapper.selectList(Wrappers.<EnergyRateManage>lambdaQuery()
                .like(StringUtils.isNotBlank(energyRateManage.getRateName()), EnergyRateManage::getRateName, energyRateManage.getRateName())
                .eq(StringUtils.isNotBlank(energyRateManage.getRateType()), EnergyRateManage::getRateType, energyRateManage.getRateType())
                .eq(StringUtils.isNotBlank(energyRateManage.getEnergyType()), EnergyRateManage::getEnergyType, energyRateManage.getEnergyType())
                .orderByDesc(EnergyRateManage::getCreateTime));
        selectPage.forEach(rate -> {
            List<EnergyRateFlow> flowList = energyRateFlowService.getBaseMapper().selectList(Wrappers.<EnergyRateFlow>lambdaQuery().eq(EnergyRateFlow::getRateId, rate.getRateId()));
            flowList.forEach(l ->{
                EnergyRateManageVo rateManageVo = new EnergyRateManageVo();
                rateManageVo.setRateName(rate.getRateName());
                rateManageVo.setEnergyType(getEnergyTypeName(Integer.parseInt(rate.getEnergyType())));
                rateManageVo.setRateStartTime(rate.getRateStartTime());
                rateManageVo.setRateEndTime(rate.getRateEndTime());
                rateManageVo.setRateType(getRateTypeName(Integer.parseInt(l.getRateType())));
                rateManageVo.setPrice(l.getPrice());
                rateManageVo.setStartTime(l.getRateStartTime());
                rateManageVo.setEndTime(l.getRateEndTime());
                resultList.add(rateManageVo);
            });
        });
        return resultList;
    }


    private static String getEnergyTypeName(int energyType) {
        switch (energyType) {
            case 1:
                return "电";
            case 2:
                return "水";
            case 3:
                return "气";
            default:
                return null;
        }
    }
    private static String getRateTypeName(int rateType) {
        switch (rateType) {
            case 1:
                return "尖电";
            case 2:
                return "峰电";
            case 3:
                return "平电";
            case 4:
                return "谷电";
            default:
                return null;
        }
    }

}